//package com.flow.common.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.flow.business.entity.User;
//import com.flow.business.service.UserService;
//import com.flow.common.exception.BusinessRuntimeException;
//import com.flow.common.utils.JavaWebToken;
//import com.flow.common.utils.StringUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.tomcat.util.http.ResponseUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Map;
//
//@Slf4j
//@WebFilter(filterName = "urlFilter", urlPatterns = "/*")
//@Component
//public class UrlFilter implements Filter {
//
//    @Value("${jwt.secret-key}")
//    public String secretKey;
//
//    @Autowired
//    UserService userService;
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        log.info("====初始化TokenFilter=====");
//    }
//
//    @Override
//    public void destroy() {
//        log.info("====销毁TokenFilter=====");
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//            throws IOException, ServletException {
//        log.info("====执行doFilter=====");
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse res = (HttpServletResponse) response;
//
//        /**
//         * 获取请求路径
//         */
//        String uri = req.getRequestURI();
//        System.out.println("===========uri=========="+uri);
//        if ("/login".equals(uri)){
//            chain.doFilter(request, response);
//            return;
//        }
//
//        String accessToken = req.getHeader("accessToken");
//        if (StringUtils.isBlank(accessToken)) {
//            accessToken = req.getParameter("accessToken");
//        }
//
//        if (StringUtil.isNotEmpty(accessToken)) {
//            Map<String, Object> stringObjectMap = JavaWebToken.parserJavaWebToken(accessToken, secretKey);
//            String sub = stringObjectMap.get("sub").toString();
//            JSONObject object = JSON.parseObject(sub);
//            String username = object.get("username") + "";
//            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("username",username);
//            queryWrapper.eq("is_delete","0");
//            User userInfo = userService.getOne(queryWrapper);
//            request.setAttribute("user",userInfo);
//        } else {
//            throw new BusinessRuntimeException("用户未登录");
//        }
//        /**
//         * 都满足条件，执行下一步
//         */
//        chain.doFilter(request, response);
//    }
//
//}
